//结算界面
class ResultScene extends BaseScene {

    lab_rule: eui.Label;

    itemGroup: eui.Group;
    btn_continue: eui.Button;

    lab_game: eui.Label;
    lab_time: eui.Label;

    img_state: eui.Image;

    baoPai: GameSceneBao;

    face: IGameTapEvent;

    constructor() {
        super();

        this.skinName = "ResultSceneSkin";
    }

    childrenCreated() {
        super.childrenCreated();

        this.top = this.bottom = this.left = this.right = 0;

        this.baoPai = new GameSceneBao;
        this.baoPai.x = 910;
        this.baoPai.y = 147;
        this.addChild(this.baoPai);

        this.baoPai.visible = GameConfig.hasBao;

        this.lab_game.text = `【${GameConfig.mahjong_name}】`;

        this.btn_continue.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
    }

    private clickHandler(e: egret.TouchEvent) {
        switch (e.currentTarget) {
            case this.btn_continue:
                this.face.onContinue();
                break;
        }
    }

    update() {
        this.updateRule();
        this.updateState(GSData.i.resultType);

        let date: Date = new Date();
        this.lab_time.text = StringUtils.getYTDByTimestamp(date.getTime()) + " " + StringUtils.getHMSByTimestamp(date.getTime());

        this.itemGroup.removeChildren();

        let item: ResultPlayerItem;
        let persons = GSData.i.result.person;
        let person: any;
        for (let i: number = 0; i < persons.length; i++) {
            person = persons[i];

            item = new ResultPlayerItem();
            this.itemGroup.addChild(item);

            item.update(person);

            item.img_line.visible = i != persons.length - 1;
        }
        this.btn_continue.visible = PublicVal.state !== StateType.replay;
    }

    //更新宝牌
    updateBao(pai: any) {
        this.baoPai.bao = pai;
    }

    updateRule() {
        this.lab_rule.text = game.ruleTexts();
    }

    updateState(type: number) {
        switch (type) {
            case 1://胜利
                GameSound.play("sound_win");
                this.img_state.source = "result_text_win";
                break;
            case 2://失败
                GameSound.play("sound_lost");
                this.img_state.source = "result_text_lose";
                break;
            case 3://流局
                GameSound.play("sound_huang");
                this.img_state.source = "result_text_liuju";
                break;
        }
    }

    clear() {
    }

    //绑定回调接口
    bindInterface(face: IGameTapEvent) {
        this.face = face;
    }
}